<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Api
 *
 * @author Administrator
 */
class Api extends Basecontroll{
    //put your code here
    public function __construct() {
        parent::__construct();
        
    }
    public function initXinQingData(){
        
        $data=array(
            "base_name"=>"一般",
            "image_name"=>"biaoqing1",
            "create_time"=>  time(),
            "listorder"=>1
        );
        $this->xinqinglistLogic->save($data);
   
        $data=array(
            "base_name"=>"难过",
            "image_name"=>"biaoqing3",
            "create_time"=>  time(),
            "listorder"=>3
        );
        $this->xinqinglistLogic->save($data);
        $data=array(
            "base_name"=>"开心",
            "image_name"=>"biaoqing4",
            "create_time"=>  time(),
            "listorder"=>4
        );
        $this->xinqinglistLogic->save($data);
        $data=array(
            "base_name"=>"生气",
            "image_name"=>"biaoqing5",
            "create_time"=>  time(),
            "listorder"=>5
        );
        $this->xinqinglistLogic->save($data);
        $data=array(
            "base_name"=>"惊恐",
            "image_name"=>"biaoqing7",
            "create_time"=>  time(),
            "listorder"=>7        );
        $this->xinqinglistLogic->save($data);
        echo "执行完毕!";
    }
    
    /**
     * 获得心情列表
     */
    public function getXinQingList(){
        $xinqinglistLogic=  $this->uselogic("Xinqinglist");
        $res=  $xinqinglistLogic->getList();
        echo json_encode($res);
    }
    /**
     * 添加用户心情
     */
    public function addXinQing(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $xinqing_id=  isset($_POST["xinqing_id"])?intval($_POST["xinqing_id"]):0;
        $msg=array();
        $userLogic=  $this->uselogic("User");
        $xinqingLogic=  $this->uselogic("Xinqinglist");
        $userxinqingLogic=  $this->uselogic("Userxinqing");
        $cityLogic=$this->uselogic("City");
        $areaLogic=$this->uselogic("Area");
        $weatherLogic=  $this->uselogic("Weather");
        $create_time=  time();
        $date_info=  getdate($create_time);
        if($user_id>0&&$xinqing_id>0){
            $user=  $userLogic->getUserInfo($user_id);
            $xinqing=$xinqingLogic->getXinQingInfo($xinqing_id);
            $weatherID="";
            if(isset($user["city_id"])){
                $city=$cityLogic->getCityInfoById($user["city_id"]);
                if(isset($city["weatherID"])&&$city["weatherID"]!=""){
                    $weatherID=$city["weatherID"];
                }
            }
            if(isset($user["area_id"])){
                $area=$areaLogic->getAreaInfoById($user["area_id"]);
                if(isset($area["weatherID"])&&$area["weatherID"]!=""){
                    $weatherID=$area["weatherID"];
                }
            }
           
            $user_xinqing_data=array(
                "user_id"=>$user_id,
                "nickname"=>$user["nickname"],
                "xinqing_id"=>$xinqing_id,
                "xinqing_name"=>$xinqing["base_name"],
                "xinqing_image"=>$xinqing["image_name"],
                "year"=>$date_info["year"],
                "month"=>$date_info["mon"],
                "day"=>$date_info["mday"],
                "create_time"=>$create_time,
                "status"=>1
            );
               
            if($weatherID!=""){
                $weather=$weatherLogic->getWeatherByWeatherID($weatherID);
                if($weather){
                    //$user_xinqing_data["weather_id"]=$weather["id"];
                    if(!empty($weather["day_wea"])&&$create_time>=intval($weather["sun_up"])&&$create_time<=  intval($weather["sun_down"])){
                        $user_xinqing_data["weather"]=$weather["day_wea"];
                        $user_xinqing_data["weather_pinyin"]=PinYin::utf8_to($weather["day_wea"]);
                    }elseif(!empty($weather["night_wea"])){
                        $user_xinqing_data["weather"]=$weather["night_wea"];
                        $user_xinqing_data["weather_pinyin"]=PinYin::utf8_to($weather["night_wea"]);
                    }else{
                        $user_xinqing_data["weather"]="无天气信息";
                        $user_xinqing_data["weather_pinyin"]="nowea";
                    }
                    $user_xinqing_data["temp"]=$weather["temp"];
                    $user_xinqing_data["aqi"]=$weather["aqi"];
                    $user_xinqing_data["aqiname"]=$weather["aqiname"];
                }
            }
         
            $res=$userxinqingLogic->save($user_xinqing_data);
            if($res){
                $msg["status"]=1;
                $msg["msg"]="记录成功!";
                exit(json_encode($msg));
            }
        }
        $msg["status"]=-1;
        $msg["msg"]="记录失败!";
        exit(json_encode($msg));
    }
    /**
     * 获得今天天气情况
     */
    public function getWeather(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $msg=array();
        $userLogic=  $this->uselogic("User");
        $cityLogic=$this->uselogic("City");
        $areaLogic=$this->uselogic("Area");
        $weatherLogic=  $this->uselogic("Weather");
        $create_time=  time();
        $date_info=  getdate($create_time);
        if($user_id>0){
            $user=  $userLogic->getUserInfo($user_id);
            $weatherID="";
            if(isset($user["city_id"])){
                $city=$cityLogic->getCityInfoById($user["city_id"]);
                if(isset($city["weatherID"])&&$city["weatherID"]!=""){
                    $weatherID=$city["weatherID"];
                }
            }
            if(isset($user["area_id"])){
                $area=$areaLogic->getAreaInfoById($user["area_id"]);
                if(isset($area["weatherID"])&&$area["weatherID"]!=""){
                    $weatherID=$area["weatherID"];
                }
            }
        }
        if($weatherID!=""){
           $weather=$weatherLogic->getWeatherByWeatherID($weatherID);
           if($weather){
               $msg["status"]=1;
               $msg["msg"]="获得天气成功";
               $weather["weather_pinyin"]=  PinYin::utf8_to($weather["weather"]);
               $weather["day_wea_pinyin"]=  PinYin::utf8_to($weather["day_wea"]);
               $weather["night_wea_pinyin"]=  PinYin::utf8_to($weather["night_wea"]);
               $msg["weather"]=$weather;
               exit(json_encode($msg));
           }
        }
        $msg["status"]=-1;
        $msg["msg"]="获得天气信息失败!";
        exit(json_encode($msg));
    }

    /**
     * 更新用户心情
     */
    public function updateXinQing(){
        
    }
    /**
     * 删除用户心情
     */
    public function deleteXinQing(){
        $user_xinqing_id= isset($_POST["user_xinqing_id"])?intval($_POST["user_xinqing_id"]):0;
        if($user_xinqing_id>0){
            $userxinqingLogic=$this->uselogic("Userxinqing");
            $userxinqingLogic->delUserXinQing($user_xinqing_id);
            $data["status"]=1;
            $data["msg"]="删除成功";
            exit(json_encode($data));
        }
        exit(json_encode(array("status"=>-1,$msg=>"数据为空","id"=>$user_xinqing_id)));
    }
    /**
     * 获得用户当天心情列表
     */
    public function userXinQingList(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $page=  isset($_POST["page"])?intval($_POST["page"]):1;
        $sdate=isset($_POST["date"])?$_POST["date"]:  date("Y-m-d")." 00:00:00";
        $edate=date("Y-m-d")." 23:59:59";
        $sdate=  strtotime($sdate);
        $edate=strtotime($edate);
        if($user_id){
            $userxinqingLogic=$this->uselogic("Userxinqing");
            $data=$userxinqingLogic->getList("user_id={$user_id} and create_time>={$sdate} and create_time<={$edate} and status=1",$page,12);
            $data["status"]=1;
            $data["msg"]="成功获得数据";
            exit(json_encode($data));
        }
        exit(json_encode(array("status"=>-1,$msg=>"数据为空")));
    }
    /**
     * 登录
     */
    public function login(){
        $user_name=  isset($_POST["name"])?Common::strEscape($_POST["name"]):"";
        $password= isset($_POST["password"])?Common::strEscape($_POST["password"]):"";
        $type= isset($_POST["type"])?intval($_POST["type"]):104;
        $city=isset($_POST["city"])?Common::strEscape($_POST["city"]):"";
        $province=isset($_POST["province"])?Common::strEscape($_POST["province"]):"";
        $area=isset($_POST["area"])?Common::strEscape($_POST["area"]):"";
        $address=isset($_POST["address"])?Common::strEscape($_POST["address"]):"";
        $lng=isset($_POST["lng"])?  doubleval($_POST["lng"]):0.0;
        $lat=isset($_POST["lat"])?doubleval($_POST["lat"]):0.0;
        $userLogic=  $this->uselogic("user");
        $msg=array();
        if($user_name==""){
            $msg["status"]=-1;
            $msg["msg"]="登录用户名为空,请输入登录用户名.";
            exit(json_encode($msg));
        }
        if($password==""){
            $msg["status"]=-2;
            $msg["msg"]="密码为空,请输入密码.";
            exit(json_encode($msg));
        }
        $res=$userLogic->login($user_name,$password,$type);
        if($res){
            $msg["status"]=1;
            $msg["msg"]="登录成功.";
            $msg["user_info"]=$res;
            $userLogic->updateLocation($res["id"],$province,$city,$area,$address,$lng,$lat);
        }else{
            $msg["status"]=-3;
            $msg["msg"]="登录失败,错误用户名或密码.";
        }
        exit(json_encode($msg));
    }
    /**
     * 计算用户昨天的温度以及温度权重情况
     */
    public function jiSuanXingQingTemp(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $todaydateinfo=  getdate();
        $yesterday=strtotime("-1 day");
        $yesterdaydateinfo=getdate($yesterday);
        $xinqingtempLogic=  $this->uselogic("Xinqingtemp");
        $msg=array();
        if($user_id>0){
            //计算昨天
            $xinqingtempLogic->updateXinQingTempInfo($user_id,$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            //计算当天
            $xinqingtempLogic->updateXinQingTempInfo($user_id,$todaydateinfo["mon"],$todaydateinfo["mday"]);
            $msg["status"]=1;
            $msg["msg"]="计算心情温度情况成功.";
            exit(json_encode($msg));
        }
        $msg["status"]=-1;
        $msg["msg"]="计算心情温度情况失败.";
        exit(json_encode($msg));
    }
   
    /**
     * 计算用户的平均温度
     */
    public function avgTemp(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $todaydateinfo=  getdate();
        $yesterday=strtotime("-1 day");
        $yesterdaydateinfo=getdate($yesterday);
        $xinqingprobabilityLogic=  $this->uselogic("Xinqingprobability");
        $msg=array();
        if($user_id>0){
            //计算昨天
            $xinqingprobabilityLogic->avgTemp($user_id,$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            //计算当天
            $xinqingprobabilityLogic->avgTemp($user_id,$todaydateinfo["mon"],$todaydateinfo["mday"]);
            $msg["status"]=1;
            $msg["msg"]="计算平均温度情况成功.";
            exit(json_encode($msg));
        }
        $msg["status"]=-1;
        $msg["msg"]="计算平均温度情况失败.";
        exit(json_encode($msg));
    }
    /**
     * 计算心情概率值
     * 
     */
    public function probXinqing(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $todaydateinfo=  getdate();
        $yesterday=strtotime("-1 day");
        $yesterdaydateinfo=getdate($yesterday);
        $xinqingprobabilityLogic=  $this->uselogic("Xinqingprobability");
        $msg=array();
        if($user_id>0){
            //计算昨天（历年来的概率）
            $xinqingprobabilityLogic->probXinqing($user_id,$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            //计算昨天当日下的概率
            $xinqingprobabilityLogic->probDayXinqing($user_id,$yesterdaydateinfo["year"],$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            //计算当天（历年来的概率）
            $xinqingprobabilityLogic->probXinqing($user_id,$todaydateinfo["mon"],$todaydateinfo["mday"]);
            //计算当天当日下的概率
            $xinqingprobabilityLogic->probDayXinqing($user_id,$todaydateinfo["year"],$todaydateinfo["mon"],$todaydateinfo["mday"]);
            //计算实时温度权重值
            $xinqingprobabilityLogic->jiSuanTempWeight($user_id);
            //计算当天的总概率值
            $xinqingprobabilityLogic->jiSuanTotal($user_id,$todaydateinfo["mon"],$todaydateinfo["mday"]);
            //计算昨天的总概率值
            $xinqingprobabilityLogic->jiSuanTotal($user_id,$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            $msg["status"]=1;
            $msg["msg"]="计算心情概率成功.";
            exit(json_encode($msg));
        }
        $msg["status"]=-1;
        $msg["msg"]="计算心情概率失败.";
        exit(json_encode($msg));
    }

        /**
     * 更新用户的物理位置
     */
    public function updateUserLocation(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $city=isset($_POST["city"])?Common::strEscape($_POST["city"]):"";
        $province=isset($_POST["province"])?Common::strEscape($_POST["province"]):"";
        $area=isset($_POST["area"])?Common::strEscape($_POST["area"]):"";
        $address=isset($_POST["address"])?Common::strEscape($_POST["address"]):"";
        $lng=isset($_POST["lng"])?  doubleval($_POST["lng"]):0.0;
        $lat=isset($_POST["lat"])?doubleval($_POST["lat"]):0.0;
        $msg=array();
        if($user_id>0){
            $userLogic=  $this->uselogic("user");
            $userLogic->updateLocation($user_id,$province,$city,$area,$address,$lng,$lat);
            $msg["status"]=1;
            $msg["postdata"]=$_POST;
            $msg["msg"]="更新物理位置成功.";
            exit(json_encode($msg));
        }
        $msg["status"]=-1;
        $msg["msg"]="更新物理位置失败.";
        exit(json_encode($msg));
    }

        /**
     * 退出登录
     */
    public function logout(){
        
    }
    /**
     * 更新用户信息
     */
    public function updateUserInfo(){
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        $key=isset($_POST["key"])?$_POST["key"]:"";
        $value=isset($_POST["value"])?$_POST["value"]:"";
        $msg=array();
        $userLogic=  $this->uselogic("user");
        if($user_id>0){
            if($key!=""&&$value!=""){
                $exist=false;
                switch ($key){
                    case "phone":
                        $exist=$userLogic->existUser($value,1);
                        break;
                    case "email":
                        $exist=$userLogic->existUser($value,2);
                        break;
                    case "base_name":
                        $exist=$userLogic->existUser($value,3);
                        break;
                }
                if($exist==FALSE){
                    $update[$key]=$value;
                    $userLogic->save($update,$user_id);
                    $msg["status"]=1;
                    $msg["key"]=$key;
                    $msg["value"]=$value;
                    $msg["msg"]="更新成功.";
                }else{
                    $msg["status"]=-2;
                    $msg["msg"]="数据已经存在,更新失败.";
                }
                exit(json_encode($msg));
            }
        }
        $msg["status"]=-1;
        $msg["msg"]="更新失败.";
        exit(json_encode($msg));
    }


    /**
     * 注册
     */
    public function register(){
        $user_name=  isset($_POST["phone"])?Common::strEscape($_POST["phone"]):"";
        $password= isset($_POST["password"])?Common::strEscape($_POST["password"]):"";
        $city=isset($_POST["city"])?Common::strEscape($_POST["city"]):"";
        $province=isset($_POST["province"])?Common::strEscape($_POST["province"]):"";
        $area=isset($_POST["area"])?Common::strEscape($_POST["area"]):"";
        $address=isset($_POST["address"])?Common::strEscape($_POST["address"]):"";
        $lng=isset($_POST["lng"])?  doubleval($_POST["lng"]):0.0;
        $lat=isset($_POST["lat"])?doubleval($_POST["lat"]):0.0;
        $userLogic=  $this->uselogic("user");
        $msg=array();
        if($user_name==""){
            $msg["status"]=-1;
            $msg["msg"]="手机号码为空,请输入手机号码.";
            exit(json_encode($msg));
        }
        if($password==""){
            $msg["status"]=-2;
            $msg["msg"]="密码为空,请输入密码.";
            exit(json_encode($msg));
        }
        if($userLogic->existUser($user_name)){
            $msg["status"]=-3;
            $msg["msg"]="用户已经注册.";
            exit(json_encode($msg));
        }
        $user_data=array(
            "phone"=>$user_name,
            "password"=>$password,
            "ip"=>  Common::getIp(),
            "create_time"=>  time(),
            "login_time"=>  time()
        );
        if($city!=""){
            $city_id=  $this->uselogic("City")->getIdByName($city);
            $user_data["city"]=$city;
            $user_data["city_id"]=$city_id;
        }
        if($province!=""){
            $province_id=  $this->uselogic("Province")->getIdByName($province);
            $user_data["province"]=$province;
            $user_data["province_id"]=$province_id;
        }
        if($area!=""){
            $area_id=  $this->uselogic("area")->getIdByName($area);
            $user_data["area_id"]=$area_id;
            $user_data["area"]=$area;
        }
        if($address!=""){
            $user_data["address"]=$address;
        }
        if($lng>0){
                $user_data["lng"]=$lng;
        }
        if($lat>0){
                $user_data["lat"]=$lat;
        }
        $res=  $userLogic->save($user_data);
        if($res){
            $userinfo=$userLogic->login($user_name,$password,104);
            $msg["status"]=1;
            $msg["msg"]="用户注册成功.";
            $msg["user_info"]=$userinfo;
            exit(json_encode($msg));
        }else{
            $msg["status"]=-4;
            $msg["msg"]="用户注册失败.";
            exit(json_encode($msg));
        }
       
    }
    
    
    
    
    /**
     * 第三方注册加登录
     */
    public function third_register(){
        $image=  isset($_POST["image"])?Common::strEscape($_POST["image"]):"";
        $province= isset($_POST["province"])?Common::strEscape($_POST["province"]):"";
        $city=isset($_POST["city"])?Common::strEscape($_POST["city"]):"";
        $nickname=isset($_POST["nickname"])?Common::strEscape($_POST["nickname"]):"";
        $email=isset($_POST["email"])?Common::strEscape($_POST["email"]):"";
        $phone=isset($_POST["phone"])?Common::strEscape($_POST["phone"]):"";
        $gender_str=isset($_POST["gender"])?Common::strEscape($_POST["gender"]):"";
        $login_type=isset($_POST["login_type"])?intval($_POST["login_type"]):106;
        $area=isset($_POST["area"])?Common::strEscape($_POST["area"]):"";
        $address=isset($_POST["address"])?Common::strEscape($_POST["address"]):"";
        $lng=isset($_POST["lng"])?  doubleval($_POST["lng"]):0.0;
        $lat=isset($_POST["lat"])?doubleval($_POST["lat"]):0.0;
        $gender=0;
        if($gender_str=="男"){
            $gender=1;
        }else if($gender_str=="女"){
            $gender=2;
        }
        $third_uid=isset($_POST["third_uid"])?Common::strEscape($_POST["third_uid"]):"";
        $userLogic=  $this->uselogic("user");
        $msg=array();
        $user_data=array(
            "image"=>$image,
            "is_third"=>1,
            "nickname"=>$nickname,
            "gender"=>$gender,
            "third_uid"=>$third_uid,
            "ip"=>  Common::getIp(),
            "login_type"=>$login_type,
            "create_time"=>  time(),
            "login_time"=>  time()
        );
        if($city!=""){
            $city_id=  $this->uselogic("City")->getIdByName($city);
            $user_data["city"]=$city;
            $user_data["city_id"]=$city_id;
        }
        if($province!=""){
            $province_id=  $this->uselogic("Province")->getIdByName($province);
            $user_data["province"]=$province;
            $user_data["province_id"]=$province_id;
        }
        if($area!=""){
            $area_id=  $this->uselogic("area")->getIdByName($area);
            $user_data["area_id"]=$area_id;
            $user_data["area"]=$area;
        }
        if($address!=""){
            $user_data["address"]=$address;
        }
        if($lng>0){
                $user_data["lng"]=$lng;
        }
        if($lat>0){
                $user_data["lat"]=$lat;
        }
        if($email!=""){
            $user_data["email"]=$email;
        }
        if($phone!=""){
            $user_data["phone"]=$phone;
        }
        if(!$userLogic->existThirdUser($third_uid)){
            $res=  $userLogic->save($user_data);
        }else{
            $userinfo=$userLogic->getThirdUser($third_uid);
            $update_data=array(
                "login_time"=>  time(),
                "ip"=>  Common::getIp(),
                "login_type"=>$login_type
            );
            if($city!=""){
                $update_data["city"]=$user_data["city"];
                $update_data["city_id"]=$user_data["city_id"];
            }
            if($province!=""){
                $update_data["province"]=$user_data["province"];
                $update_data["province_id"]=$user_data["province_id"];
            }
            if($area!=""){
                $update_data["area_id"]=$user_data["area_id"];
                $update_data["area"]=$user_data["area"];
            }
            if($address!=""){
                $update_data["address"]=$user_data["address"];
            }
            if($lng>0){
                $update_data["lng"]=$user_data["lng"];
            }
            if($lat>0){
                $update_data["lat"]=$user_data["lat"];
            }
            if($image!=$userinfo["image"]){
                $update_data["image"]=$image;
            }
            if(intval($userinfo["id"])>0){
                $userLogic->save($update_data,$userinfo["id"]);
            }
        }
            $userinfo=$userLogic->getThirdUser($third_uid);
            $msg["status"]=1;
            $msg["msg"]="第三方登录成功.";
            $msg["user_info"]=$userinfo;
            exit(json_encode($msg));
       
    }
    public function getXinQing(){
        $msg=array();
        $user_id=  isset($_POST["user_id"])?intval($_POST["user_id"]):0;
        if($user_id>0){
            $xinqingprobabilityLogic=  $this->uselogic("Xinqingprobability");
            $xinqinglistLogic=$this->uselogic("Xinqinglist");
            
            $dateinfo=  getdate();
            $yesterday=strtotime("-1 day");
            $yesterdaydateinfo=getdate($yesterday);
            $tomorrow=strtotime("+1 day");
            $tomorrowdateinfo=getdate($tomorrow);
            $data=array();
            $userweather=$xinqingprobabilityLogic->getUserXinQing($user_id,$dateinfo["mon"],$dateinfo["mday"]);
            if($userweather){
                $xinqing=$xinqinglistLogic->getXinQingInfo($userweather["xinqing_id"]);
                $userweather["image_name"]=$xinqing["image_name"];
                $data["today"]=$userweather;
            }else{
                $data["today"]=FALSE;
            }
            $userweather2=$xinqingprobabilityLogic->getUserXinQing($user_id,$yesterdaydateinfo["mon"],$yesterdaydateinfo["mday"]);
            if($userweather2){
                $xinqing2=$xinqinglistLogic->getXinQingInfo($userweather2["xinqing_id"]);
                $userweather2["image_name"]=$xinqing2["image_name"];
                $data["yesterday"]=$userweather2;
            }else{
                $data["yesterday"]=FALSE;
            }

            $userweather3=$xinqingprobabilityLogic->getUserXinQing($user_id,$tomorrowdateinfo["mon"],$tomorrowdateinfo["mday"]);
            if($userweather3){
                $xinqing3=$xinqinglistLogic->getXinQingInfo($userweather3["xinqing_id"]);
                $userweather3["image_name"]=$xinqing3["image_name"];
                $data["tomorrow"]=$userweather3;
            }else{
                 $data["tomorrow"]=FALSE;
            }
            $msg["status"]=1;
            $msg["msg"]="获得用户心情成功.";
            $msg["data"]=$data;
            exit(json_encode($msg));
        }
        $msg["status"]=-1;
        $msg["msg"]="获得用户心情失败.";
        exit(json_encode($msg));
    }
    
}
